<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1000" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true" draggable>
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="120px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="客户编号" prop="customer_code">
                        <el-input v-model="form.customer_code" placeholder="客户编号(不填写自动生成)" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="客户名称" prop="customer_name">
                        <!-- <el-input v-model="form.customer_name" @blur="form.help_remember=this.common.getPinyinInitials(form.customer_name)" placeholder="请输入客户名称" clearable></el-input> -->
                        <el-input v-model="form.customer_name" @blur="pinyin" placeholder="请输入客户名称" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="客户助记码" prop="help_remember">
                        <el-input v-model="form.help_remember" placeholder="请输入客户助记码" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="地址" prop="address">
                        <el-input v-model="form.address" placeholder="请输入地址" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="联系人" prop="contacts">
                        <el-input v-model="form.contacts" placeholder="请输入联系人" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="联系人电话" prop="mobile">
                        <el-input v-model="form.mobile" placeholder="请输入联系人电话" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="开户银行" prop="open_bank">
                        <el-input v-model="form.open_bank" placeholder="请输入开户银行" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="账号" prop="account">
                        <el-input v-model="form.account" placeholder="请输入账号" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="来源" prop="source">
                        <el-select v-model="form.source" filterable style="width: 100%" placeholder="请选择来源">
                        <el-option v-for="item in sourceList" :key="item.id" :label="item.source_name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="业务员" prop="salesman">
                        <el-select v-model="form.salesman" placeholder="请选择" filterable clearable>
                            <el-option v-for="item in adminList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="税号" prop="tax_number">
                        <el-input v-model="form.tax_number" placeholder="请输入税号" clearable type="number" @focus="onFocus($event)"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="状态" prop="system_disable">
                        <el-switch v-model="form.system_disable" :active-value="0" :inactive-value="1" inline-prompt active-text="启用" inactive-text="禁用"></el-switch>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="附件" prop="enclosure">
                <sc-upload-file v-model="form.enclosure">
                    <el-button type="primary" icon="el-icon-upload">选择文件</el-button>
                </sc-upload-file>
            </el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" placeholder="请输入备注" clearable type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增客户',
				edit: '编辑客户',
				show: '查看客户'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				id: "",
				customer_code: "",
				customer_num: "",
                help_remember: "",
				customer_name: "",
				address: "",
				source: "",
				enclosure: "",
				remark: "",
				system_disable: 0,
				// customer_detail: [],
				salesman: "",
				open_bank: "",
				account: "",
				tax_number: "",
				contacts: "",
				mobile: "",
			},
			//验证规则
			rules: {
                customer_name: [{required: true, message: '请输入客户名称'}],
				// customer_code: [{required: true, message: '请输入客户代码'}],
				// customer_detail: [{required: true, message: '请添加联系人'}],
			},
			addTemplate: {},
			sourceList: [],
			adminList:[]
		}
	},
	mounted() {
		this.getSource()
		this.getUser()
	},
	methods: {
        async pinyin(){
            var res = await this.$API.goods.getPinyin.get({val:this.form.customer_name});
            this.form.help_remember = res.data.py_code
        },
		/**获取员工*/
		async getUser() {
			var res = await this.$API.system.admin.all.get();
			this.adminList = res.data;
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
                    var res = await this.$API.client.client[this.form.id ? 'edit' : 'add'].post(this.form);
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				} else {
					return false;
				}
			})
		},
		getPid(data) {
			this.form.type_id = data
		},
		//表单注入数据
		async setData(data) {
			this.form.id = data.id
			var res = await this.$API.client.client.detail.get({id: data.id});
			console.log(res);
			this.form.customer_code = res.data.customer_code
			this.form.customer_num = res.data.customer_num
			this.form.help_remember = res.data.help_remember
			this.form.customer_name = res.data.customer_name
			this.form.address = res.data.address
			this.form.source = Number(res.data.source)
			this.form.enclosure = res.data.enclosure
			this.form.remark = res.data.remark
			this.form.system_disable = res.data.system_disable
			this.form.salesman = Number(res.data.salesman)
			this.form.open_bank = res.data.open_bank
			this.form.account = res.data.account
			this.form.tax_number = res.data.tax_number
			this.form.contacts = res.data.contacts
			this.form.mobile = res.data.mobile
		},
		// 获取分类
		async getSource() {
			var res = await this.$API.client.source.all.get();
			this.sourceList = res.data
		},
		onFocus(event) {
			event.target.select();
		},
	}
}
</script>

<style></style>
